// 商家
const {userledger,sequelize,merchantInfo} = require('../../dbConfig/index.js')
// 注册商家信息
const registerMerchant = async (ctx,next) => {
	const body = ctx.request.body
	if(body.user_name){
		const jiance = await userledger.findAll({where:{user_name:body.user_name}})
		if(jiance.length){
			ctx.body = {code:1,data:null,msg:'用户名以被使用'}
			return
		}
	}
	const transaction = await sequelize.transaction();
	const user = await userledger.create({ user_name:body.user_name, user_password:'25d55ad283aa400af464c76d713c07ad',is_flg:body.is_flg,creator_id:ctx.state.user_id }, { transaction });
	await merchantInfo.create({ 
		user_phone: body.user_phone, 
		full_name:body.full_name,
		person_responsible:body.person_responsible,
		user_id: user.Id,
		province:body.province,
		city:body.city,
		county:body.county,
		town:body.town,
		village:body.village,
		province_text:body.province_text,
		city_text:body.city_text,
		county_text:body.county_text,
		town_text:body.town_text,
		village_text:body.village_text,
		added_services:body.added_services.join(','),
		merchant_type:body.merchant_type,
		premium_received:body.premium_received,
		due_date:body.due_date,
		bank_number:body.bank_number,
		payment_status:body.payment_status,
		position_coordinate:body.position_coordinate
	}, { transaction });
	await transaction.commit();
	ctx.body = {
		code:200,
		data:null,
		msg:"注册成功,密码:12345678,请尽快修改密码"
	}
}
// 获取商家用户 超管获取所有 业务员只能获取自己创建的商家  TODO 查询条件没加
const getMerchantList = async (ctx,next) => {
	const user = await userledger.findOne({where:{Id:ctx.state.user_id}})// 当前登录用户的id
	  // 基础查询配置
	  const queryOptions = {
		include: [{
		  model: userledger,
		  as: 'userInfo',
		  required: true,
		  attributes: ['user_name', 'is_flg', 'creator_id'],
		  include: [{
			model: userledger,
			as: 'creatorInfo',
			required: true,
			attributes: ['user_name', 'is_flg']
		  }]
		}]
	  };
      if(user.is_flg==='3'){
		queryOptions.include[0].include[0].where={
			creator_id:user.creator_id
		}
	  }
	  const merchants = await merchantInfo.findAll(queryOptions);
	   // 格式化返回数据
	   const formattedMerchants = merchants.map(merchant => {
		const plainMerchant = merchant.get({ plain: true });
		return {
		  ...plainMerchant,
		  user_name: plainMerchant.userInfo.user_name,
		//   is_flg: plainMerchant.userInfo.is_flg,
		  creator_name: plainMerchant.userInfo.creatorInfo.user_name,
		  creator_is_flg: plainMerchant.userInfo.creatorInfo.is_flg,
		  userInfo: undefined // 移除嵌套数据
		};
	  });
	ctx.body={
		code:200,
		data:formattedMerchants,
		msg:'成功'
	}
}
const handelBusinesTime = async (ctx,next)=>{
	const body = ctx.request.body
	const user_id = ctx.state.user_id
	const res = await merchantInfo.update({
		business_start_time:body.business_start_time,
		business_ent_time:body.business_ent_time
	},{
		where:{
			user_id
		}
	})
	ctx.body = {
		code:200,
		data:null,
		msg:'成功'
	}
}
// 审核
const merchantOpinion = async (ctx,next)=>{
	const body = ctx.request.body
	await merchantInfo.update({
		payment_status:'1'
	},{
		where:{
			Id:body.Id
		}
	})
	ctx.body = {
		code:200,
		data:null,
		msg:'成功'
	}
}
// 密码修改
const editPass = async (ctx,next)=>{
	const body = ctx.request.body
	const user_id = ctx.state.user_id
	const oldPass = await userledger.findOne({
		where:{
			Id:user_id
		}
	})
	if(oldPass.user_password!==body.oldP){
		ctx.body={code:1,data:null,msg:'原密码错误'}
		return
	}
	await userledger.update({
		user_password:body.newP
	},{
		where:{
			Id:user_id
		}
	})
	ctx.body = {code:200,data:null,msg:'修改成功'}
}
module.exports = {
	registerMerchant,
	getMerchantList,
	handelBusinesTime,
	merchantOpinion,
	editPass
}